import json

# dict1 = json.load(open('门诊.json', mode='r', encoding='utf-8'))
# dict2 = dict(sorted(dict1.items(), key=lambda x:x[1], reverse=True))
# json.dump(dict2, open('门诊2.json', mode='w', encoding='utf-8'), ensure_ascii=False)


"""
累计比例
"""
# name_list = ['影像学检查2.json', '检查报告2.json', '检查结果2.json', '门诊2.json']

# for name in name_list:
#     result_dict = {}
#     current_value = 0
#     content = json.load(open(name, mode='r', encoding='utf-8'))
#     sum_all = sum(content.values())
#     for name, value in content.items():
#         print(name, value)
#         current_value += value
#         result_dict['name'] = round(value / sum_all, 2)
#     print(result_dict)
#     break
    #print(sum_all)

name_list = ['手术.json']
import numpy as np
for f_name in name_list:
    output_f_name = '统计比例' + f_name
    with open(f_name) as f:
        data = eval(f.read())
        data = [(k, data[k]) for k in data]

    data = sorted(data, key=lambda x: x[1], reverse=True)
    s = np.cumsum([x[1] for x in data])
    s = s * 1.0
    csp = s / s[-1]
    with open(output_f_name, 'w') as f:
        json.dump([(b[0], b[1], a) for a, b in zip(csp, data)], f, ensure_ascii=False)